<?php

namespace App\Models;

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;

class Cart extends Model
{
    protected $table = 'cart';

    //查询用户购物车所有信息
    public function sel()
    {
        if(session()->has('homeuser')) {
            $id = session('homeuser');
            $ob = $this->join('product', 'cart.product_id', '=', 'product.id')
                ->where('cart.user_id', $id)->select('cart.id','product_id','specifications','preview','cart.count','name','price','stock','cart.status')->get();
//            dd($ob);

                return $ob;

        }else {
            return null;
        }
    }

    //查询用户购物车被选中商品信息
    public function selPitch()
    {
        if(session()->has('homeuser')) {
            $id = session('homeuser');
            $ob = $this->join('product', 'cart.product_id', '=', 'product.id')
                ->where([['cart.user_id', $id],['cart.status',2]])->select('cart.id','product_id','specifications','preview','cart.count','name','price','stock','cart.status')->get();
            if ($ob->count() != 0) {
                return $ob;
            } else {
                return null;
            }
        }else {
            return null;
        }
    }

    //查询用户购物车被选中商品的总价格
    public function sumCart()
    {
        $bo = $this->sel();
        $sum = 0;
        if($bo == null){
            return $sum;
        }else{
            foreach ($bo as $v){
                if($v->status == 2){
                    $sum += ($v->price * $v->count);
                }
            }
        }
        return $sum;
    }

    //查询用户购物车单个商品信息
    public function selfirst(Request $request)
    {
        $id = $request->input('id');

        $ob = $this->join('product', 'cart.product_id', '=', 'product.id')
            ->where('cart.id', $id)->select('cart.id','product_id','specifications','preview','cart.count','name','price','stock','cart.status')->first();
        if ($ob) {
            return $ob;
        } else {
            return null;
        }

    }

    //删除用户购物车单个商品信息
    public function delfirst(Request $request)
    {
        $id = $request->input('id');
        $ob = $this->where('cart.id', $id)->delete();
        if ($ob) {
            return $ob;
        } else {
            return null;
        }

    }

    //清空购物车
    public function doqing()
    {
        if(session()->has('homeuser')) {
            $id = session('homeuser');
            $ob = $this->where('user_id', $id)->delete();
            if ($ob) {
                return $ob;
            } else {
                return null;
            }
        }else {
            return null;
        }
    }

    //删除购物车中被选中的商品信息
    public function delPitch()
    {
        if(session()->has('homeuser')) {
            $id = session('homeuser');
            $ob = $this->where([['user_id', $id],['status',2]])->delete();
            if ($ob) {
                return $ob;
            } else {
                return null;
            }
        }else {
            return null;
        }
    }

    //查找当前用户购物车商品的数量
    public function donum(){
        if(session()->has('homeuser')) {
            $id = session('homeuser');
            $ob = $this->where('user_id', $id)->sum("count");
            if ($ob) {
                return $ob;
            } else {
                return 0;
            }
        }else {
            return 0;
        }
    }
}
